package com.haoze.nurseapp.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.common.utils.HttpClientUtil;
import com.haoze.common.utils.JSONUtils;
import com.haoze.common.utils.UUIDUtil;
import com.haoze.nurseapp.dto.specimen.SpecilmenDetailLisDto;
import com.haoze.nurseapp.dto.specimen.SpecimenReadyDto;
import com.haoze.nurseapp.listen.ExcelUtil;
import com.haoze.nurseapp.model.DoubleCheckLogEntity;
import com.haoze.nurseapp.model.SysTestTubeColorEntity;
import com.haoze.nurseapp.service.SpecimenService;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.ws.rs.POST;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangze
 * @date 2020-05-18 15:41.
 **/
@Api(value = "检验标本采集")
@RequestMapping("/nurseapp/labQrCode")
@RestController
public class LabQrCodeController {

	@Resource
	private SpecimenService specimenService;

	@Autowired
	private ExcelUtil excelUtil;


	@GetMapping("/scan/specimen")
	@ApiOperation(value = "扫描标本条码")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sepcimenNum", value = "标本条码", required = true)
	})
	public Result querySpecimen(@RequestParam String sepcimenNum) {
		return ResultGenerator.genOkResult(this.specimenService.querySpecimen(sepcimenNum, null));
	}

	@PostMapping("/ready")
	@ApiOperation(value = "准备完毕")
	public Result ready(@RequestBody List<SpecimenReadyDto>  readyDtos) {
		return specimenService.specimenReady(readyDtos);
	}

	@PostMapping("/finish")
	@ApiOperation(value = "采集结束")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "specimenNums", value = "标本条码(数组)", required = true)
	})
	public Result finish(@RequestBody List<String> specimenNums) {
		// 执行采集结束流程
		return specimenService.specimenFinish(specimenNums);
	}


	@GetMapping("/queryLabItems")
	@ApiOperation(value = "获取标本对应的检查项目")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "specimen", value = "标本条码", required = true)
	})
	public Result queryLabItems(String specimen) {
		return ResultGenerator.genOkResult(specimenService.queryLabList(specimen));
	}


	public static void main(String[] args) {
		String url = "http://172.20.110.120:4003/lisapi/getspecimen";
		Map<String, Object> param = new HashMap<>(4);
		param.put("InpatNum", "1144673");
		param.put("RegNum", "");
		param.put("SeNum", "");
		param.put("BeginDate", "2020-06-04 00:00:00");
		param.put("EndDate", "2020-06-04 23:59:00");
		try {
			JSONObject jsonObject = HttpClientUtil.sendPost(url, param);
			if (jsonObject.getString("Data") != null && StringUtils.isNotBlank(jsonObject.getJSONObject("Data").getString("RowNums"))) {
				System.out.println(jsonObject.getString("Data"));
				List<SpecilmenDetailLisDto> dtos = JSON.parseArray(jsonObject.getJSONObject("Data").getString("RowNums"), SpecilmenDetailLisDto.class);
				System.out.println(JSONUtils.beanToJson(dtos));
				System.out.println("数量:"+dtos.size());
			}
		} catch (Exception e) {
			System.out.println("111111111111111");
			return;
		}
		System.out.println("222222222222222");
	}


	@PostMapping("/saveNurseDoubleCheckLog")
	@ApiOperation(value = "副护士的开始核对")
	public Result saveDoubleCheckLog(@RequestBody DoubleCheckLogEntity doubleCheckLogEntity) {
		return specimenService.saveDoubleCheckLog(doubleCheckLogEntity);
	}

	@PostMapping("/checkDoctorDoubleCheckLog")
	@ApiOperation(value = "护士的开始核对")
	public Result checkDoctorDoubleCheckLog(@RequestBody DoubleCheckLogEntity doubleCheckLogEntity) {
		return specimenService.checkDoctorDoubleCheckLog(doubleCheckLogEntity);
	}


	@PostMapping("/nurseDoubleCheck")
	@ApiOperation(value = "双人核对完毕")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "specimenNums", value = "标本条码(数组)", required = true)
	})
	public Result nurseDoubleCheck(@RequestBody DoubleCheckLogEntity doubleCheckLogEntity) {
		// 执行采集结束流程
		return specimenService.nurseDoubleCheck(doubleCheckLogEntity);
	}


	@PostMapping(value = "/uploadExcel", consumes = "multipart/*", headers = "content-type=multipart/form-data")
	@ApiOperation(value = "上传lis数据")
	public Result uploadAccount(@ApiParam(value = "用户信息Excel导入数据", required = true) MultipartFile file) throws Exception {
		List<SysTestTubeColorEntity> dtoList = excelUtil.readExcelFileToDTO(file, SysTestTubeColorEntity.class);
		System.out.println(dtoList);
		if(CollectionUtils.isNotEmpty(dtoList)){
			for(SysTestTubeColorEntity dto:dtoList){
				dto.setTestTubeColorId(UUIDUtil.randomString());
				dto.initAdd();
				//根据
				String basicdatavalueId = specimenService.selectHspBasicDataValue(dto.getVascularType());
				dto.setVascularType(basicdatavalueId);
				specimenService.insertSysTestTubeColor(dto);
			}
		}
		return ResultGenerator.genOkResult();
	}


}
